package cc.Controller;

import cc.Entity.Category;
import cc.Entity.ContentInfo;
import cc.Entity.Results;
import cc.ServiceImpl.ContentServiceImpl;
import cc.Utils.ListToResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ContentController {
    @Autowired
    private ContentServiceImpl contentServiceImpl;

    //发布文章
    @ResponseBody
    @RequestMapping(value = "/publish",method = RequestMethod.POST)
    public Results addContent(@RequestBody ContentInfo contentInfo){
        contentInfo.setInfoPv("0");
        contentInfo.setGreatNumber("55");

        if(this.contentServiceImpl.addContent(contentInfo)!=0){
            return  ListToResultUtil.getResult(true,"00","成功",null);
        }
        return  ListToResultUtil.getResult(false,null,"失败",null);
    }
    //后台搜索框搜索，作者，标题，id
    @ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Results getContent(@RequestBody ContentInfo contentInfo,@RequestParam(value = "pn",defaultValue = "1") int pn){
        PageHelper.startPage(pn, 10);
        List<ContentInfo> infos = this.contentServiceImpl.getContent(contentInfo);
        if(infos==null || infos.size()==0){

            return  ListToResultUtil.getResult(false,null,"失败",null);
        }
        PageInfo pageInfo = new PageInfo(infos, 5);
        List<Object> list = new ArrayList<>();
        list.add(pageInfo);
        return  ListToResultUtil.getResult(true,"00","成功",list);
    }
    //删除文章
    @ResponseBody
    @RequestMapping(value = "/delete/{infoId}",method = RequestMethod.DELETE)
    public Results delContent(@PathVariable("infoId") String infoId){
        if(this.contentServiceImpl.delContent(infoId)!=0){
            return  ListToResultUtil.getResult(true,"00","成功",null);
        }
        return  ListToResultUtil.getResult(false,null,"失败",null);
    }
   /* @ResponseBody
    @RequestMapping(value = "/paper",method = RequestMethod.POST)
    public Msg uploadPaperImage(MultipartFile file, HttpServletRequest request){
        String url = "";
        try {
            paperService.addPaper(file,request);
           *//*
           上传每张试卷应对每张试卷进行切大块操作
            *//*
            return Msg.success();
        }catch (Exception e){
            e.printStackTrace();
            return Msg.fail().add("msg",e.getMessage());
        }*/
    //查看单条新闻内容
   @ResponseBody
   @RequestMapping(value = "/alone",method = RequestMethod.POST )
   public Results getConContent(@RequestParam(value = "infoId") String infoId, HttpServletRequest request){
       if(contentServiceImpl.getConContent(infoId,request )==null){
           return  ListToResultUtil.getResult(false,null,"失败",null);
       }
       List<ContentInfo > list = new ArrayList<>();
       ContentInfo contentInfo = contentServiceImpl.getConContent(infoId,request);
       String pv = contentInfo.getInfoPv();
       String s;
       s = request.getSession().getAttribute(infoId).toString();
       if(s==null){
       pv = pv +"-"+1;
       }else {
           pv = pv+"-"+s;
       }
       contentInfo.setInfoPv(pv);
       list.add(contentInfo);

       return  ListToResultUtil.getResult(true,"00","成功",list);
   }
    //通过type来查找，前端震情信息，新闻事件，地震科普
    @ResponseBody
    @RequestMapping(value = "/type",method = RequestMethod.GET)
    public Results getContentbytype(@RequestParam(value = "infoType") String infoType,
                                    HttpServletRequest request,@RequestParam(value = "pn",defaultValue = "1") int pn){

         List<ContentInfo> list = new ArrayList<>();

        PageHelper.startPage(pn, 10);
        List<ContentInfo> contentInfos  =contentServiceImpl.getContentByType(infoType);

        for(ContentInfo contentInfo :contentInfos){
            String pv = contentInfo.getInfoPv();
            String s = (String) request.getSession().getAttribute(infoType);
            if(s==null){
                pv = pv +"-"+1;
            }else {
                pv = pv+"-"+s;
            }
           contentInfo.setInfoPv(pv);
        }
        if(contentInfos==null){
            return  ListToResultUtil.getResult(false,null,"失败",null);
        }
        PageInfo pageInfo = new PageInfo(contentInfos, 5);
        List list1 = new ArrayList();
        list1.add(pageInfo);
        return  ListToResultUtil.getResult(true,"00","成功",list1);
    }
    //在后台查询，登录后直接调用这个借口
    @ResponseBody
    @RequestMapping(value = "/affer",method = RequestMethod.POST)
    public Results getBackContent(@RequestParam(value = "pn",defaultValue = "1") int pn){
        PageHelper.startPage(pn, 10);
       /*if(contentServiceImpl.getBackContent(infoHeadline)==null || contentServiceImpl.getBackContent(infoHeadline).size()==0){
            return  ListToResultUtil.getResult(false,null,"失败",null);
        }*/
        PageInfo pageInfo = new PageInfo(contentServiceImpl.getBackContent(), 5);
        List list1 = new ArrayList();
        list1.add(pageInfo);
        return  ListToResultUtil.getResult(true,"00","成功",list1);
    }
    //编辑
    @ResponseBody
    @RequestMapping(value = "/editor",method = RequestMethod.POST)
     public Results updateContentById(@RequestParam("infoId") String infoId, @RequestParam("infoContent") String infoContent){
       if(contentServiceImpl.updateContentById(infoId,infoContent) == 0 ){
        return ListToResultUtil.getResult(false,null,"失败",null);
       }else{
           return  ListToResultUtil.getResult(true,"00","成功",null);
       }
    }
    //点赞数
    @ResponseBody
    @RequestMapping(value = "/greatNum/{infoId}",method = RequestMethod.PUT)
    public Results updateGreatNum(@PathVariable("infoId") String infoId){
        if(contentServiceImpl.updateGreatNum(infoId)== 0 ){
            return ListToResultUtil.getResult(false,null,"失败",null);
        }else{
            return  ListToResultUtil.getResult(true,"00","成功",null);
        }
    }
    //通过类型来查category,后端登录后默认选择所有，当选择其他类别时，才调用这个借口
    @ResponseBody
    @RequestMapping(value = "/category/{info_category}",method = RequestMethod.GET)
    public Results  getBackContentByCategory(@PathVariable("info_category") String info_category,@RequestParam(value = "pn",defaultValue = "1") int pn){
        PageHelper.startPage(pn, 10);
        List<Category> infos = this.contentServiceImpl.getBackContentByCategory(info_category);
        if(infos == null || infos.size() == 0){
            return ListToResultUtil.getResult(false,null,"失败",null);
        }else{
            PageInfo pageInfo = new PageInfo(infos, 5);
            List<Object> list = new ArrayList<>();
            list.add(pageInfo);
            return  ListToResultUtil.getResult(true,"00","成功",list);
        }
    }
    }



